/**
* @author Ashish Gupta
* CVSSP, University of Surrey
* Copyright (c) 2010 Ashish Gupta
* All rights reserved.
*/
/**
 * 
 */
package hash;

import image.ImageSet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import configuration.VisualRecognitionConfiguration;

/**
 * @author Ashish
 *
 */
public class CellSpace implements Hash {
	private ArrayList<Cell> cells = new ArrayList<Cell>();
	private ArrayList<ArrayList<String>> hashCells = new ArrayList<ArrayList<String>>();
	
	public CellSpace(VisualRecognitionConfiguration config, ImageSet imageSet) throws IOException{
		readHashCells(config);
		setCells(imageSet);
	}
	/* (non-Javadoc)
	 * @see hash.Hash#readHashCells()
	 */
	@Override
	public void readHashCells(VisualRecognitionConfiguration config) throws IOException {
		File file = new File(config.lshTableIndexURI);
		FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr);
		String line = null;
		while((line=br.readLine())!=null){
			String[] inLine = line.split(",");
			ArrayList<String> temp = new ArrayList<String>(inLine.length);
			for(int i=0;i<inLine.length;i++){
				temp.add(inLine[i]);
			}
			hashCells.add(temp);
		}
		br.close();
		fr.close();
	}

	/**
	 * @param cells the cells to set
	 */
	public void setCells(ImageSet imageSet) {
		for(int i=0;i<hashCells.size();i++){
			Cell cell = new Cell();
			cell.setCellIdx(i);
			for(int j = 0; j < hashCells.get(i).size();j++){
				int fvIdx = Integer.parseInt(hashCells.get(i).get(j));
				cell.addFeatureVectorIdentity(imageSet.getFeatureVectorIdentities().get(fvIdx));
			}
			cell.calcClassType(imageSet.getImageList());
			cells.add(cell);
		}
	}

	/**
	 * @return the cells
	 */
	public ArrayList<Cell> getCells() {
		return cells;
	}

	/**
	 * @param hashCells the hashCells to set
	 */
	public void setHashCells(ArrayList<ArrayList<String>> hashCells) {
		this.hashCells = hashCells;
	}

	/**
	 * @return the hashCells
	 */
	public ArrayList<ArrayList<String>> getHashCells() {
		return hashCells;
	}

	

}
